package pl.ptemich.nov2k13.engine;

import java.util.List;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Polygon;

import pl.ptemich.nov2k13.engine.HexBoard.HexType;

/**
 * See 
 * http://www.codeproject.com/Articles/14948/Hexagonal-grid-for-games-and-other-projects-Part-1
 * http://www.redblobgames.com/grids/hexagons/
 * 
 * @author Brutusek
 *
 */
public abstract class AbstractHex implements Hex {

  private OffsetCooridinate offsetCoordinate;

  protected SimplePoint center;

  protected int side;

  protected SimplePoint[] vertexes;
  
  // - the /
  protected float a1Factor;

  // - the \
  protected float a2Factor;

  // top left wall
  protected float b1tFactor;

  // bottom right wall
  protected float b1bFactor;

  // bottom left wall
  protected float b2tFactor;

  // top right wall
  protected float b2bFactor;

  //protected float circle;  
  
  protected float r;
  
  protected float h;

  protected HexState hexStates; 
  
  protected HexType hexType;
  
  protected List<WorldObject> worldObjects;
  
  protected void init(int side, float posX, float posY, int hexX, int hexY, float h, float r, HexType hexType) {
    this.side = side;
    this.center = new SimplePoint(posX, posY);

    offsetCoordinate = new OffsetCooridinate(hexX, hexY);    
    
    this.h = h;
    this.r = r;

    this.hexType = hexType;
    
    this.hexStates = new HexState();
  }
  
  public static float calculateH(int side) {
    return (float) Math.sin(Math.toRadians(30)) * side;
  }

  public static float calculateR(int side) {
    return (float) Math.cos(Math.toRadians(30)) * side;
  }

  public void draw(Graphics g) {
    if (hexStates.contains(HexProperty.LEFT_SELECTED)) {
      g.setColor(new Color(0, 0, 118));
      Polygon poly = new Polygon();
      poly.addPoint(vertexes[0].x, vertexes[0].y);
      poly.addPoint(vertexes[1].x, vertexes[1].y);
      poly.addPoint(vertexes[2].x, vertexes[2].y);
      poly.addPoint(vertexes[3].x, vertexes[3].y);
      poly.addPoint(vertexes[4].x, vertexes[4].y);
      poly.addPoint(vertexes[5].x, vertexes[5].y);
      g.fill(poly);
    } else if (hexStates.contains(HexProperty.RIGH_SELECTED))  {
      g.setColor(new Color(0, 255, 0));
      Polygon poly = new Polygon();
      poly.addPoint(vertexes[0].x, vertexes[0].y);
      poly.addPoint(vertexes[1].x, vertexes[1].y);
      poly.addPoint(vertexes[2].x, vertexes[2].y);
      poly.addPoint(vertexes[3].x, vertexes[3].y);
      poly.addPoint(vertexes[4].x, vertexes[4].y);
      poly.addPoint(vertexes[5].x, vertexes[5].y);
      g.fill(poly);      
    }

    if (hexStates.contains(HexProperty.HOVER)) {
      g.setColor(Color.white);
      g.setLineWidth(2);
    } else {
      g.setColor(new Color(194, 228, 246));
      g.setLineWidth(1);
    }
        
    g.drawLine(vertexes[0].x, vertexes[0].y,
               vertexes[1].x, vertexes[1].y);
    g.drawLine(vertexes[1].x, vertexes[1].y,
               vertexes[2].x, vertexes[2].y);
    g.drawLine(vertexes[2].x, vertexes[2].y,
               vertexes[3].x, vertexes[3].y);
    g.drawLine(vertexes[3].x, vertexes[3].y,
               vertexes[4].x, vertexes[4].y);
    g.drawLine(vertexes[4].x, vertexes[4].y,
               vertexes[5].x, vertexes[5].y);
    g.drawLine(vertexes[5].x, vertexes[5].y,
               vertexes[0].x, vertexes[0].y);

    g.setColor(Color.white);
    CubeCooridnate cc = offsetCoordinate.getCubeCoordinate(); 
    //g.drawString(String.valueOf(cc.x), vertexes[0].x, vertexes[0].y);
    //g.drawString(String.valueOf(cc.y), vertexes[0].x + 12, vertexes[0].y + 8);
    //g.drawString(String.valueOf(cc.z), vertexes[0].x, vertexes[0].y + 18);
    //g.drawString(r + ":" + center.x + ":" + center.y, vertexes[0].x, vertexes[0].y + 10);
    g.drawString(offsetCoordinate.c + ":" + offsetCoordinate.r, vertexes[0].x, vertexes[0].y);
    g.drawString(offsetCoordinate.getAxialCoordinate().x + ":" + offsetCoordinate.getAxialCoordinate().y, vertexes[0].x, vertexes[0].y + 15);
  }
  
  
  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder("HEX:");
    sb.append(center.x).append("-").append(center.y)
      .append(", ").append(offsetCoordinate.toString()).append("\n")
      .append("a:").append(a1Factor)
      .append(" b1:").append(b1tFactor)
      .append(" b2:").append(b1bFactor).append("\n")
      .append("a:").append(a2Factor)
      .append(" b1:").append(b2tFactor)
      .append(" b2:").append(b2bFactor).append("\n")
      .append("offset:").append(offsetCoordinate).append(", ")
      .append("cube:").append(offsetCoordinate.getCubeCoordinate()).append(", ")
      .append("axial:").append(offsetCoordinate.getAxialCoordinate()); 
    return sb.toString();
  }  
  
  @Override
  public final OffsetCooridinate getOffsetCoordinate() {
    return offsetCoordinate;
  }
  
  @Override
  public HexState getHexState() {
    return hexStates;
  }

  public List<WorldObject> getWorldObjects() {
    return worldObjects;
  }  

  
}
